package cc.linker.steplink.crm.merchant.service;

import cc.linker.steplink.crm.merchant.dao.PlatformDao;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PlatformService {

    @Autowired
    private PlatformDao platformDao;

    public List<Map<String,Object>> getList() {
        List<Map<String, Object>> list = platformDao.getList();
        return list;
    }


    public void savePlatform(Map<String, Object> map) {
        if(null!=platformDao.getInfoByName(map)){
            throw GlobalException.build("保存失败：平台名称重复");
        }
        Object platFormIdObj = map.get("platFormId");
        if(platFormIdObj!=null && platFormIdObj.toString()!=""){
            //编辑
            platformDao.updatePlatform(map);
        }else {
            //新增
            platformDao.addPlatform(map);
        }
    }

    public void removePlatform(Long platFormId) {
        platformDao.removePlatform(platFormId);
    }


    public Map<String, Object> getPlatformInfo(Long platFormId) {
        return platformDao.getPlatformInfo(platFormId);
    }


    public List<Map<String,Object>> getBussinessList(Long platFormId) {
        List<Map<String, Object>> list = platformDao.getBussinessList(platFormId);
        return list;
    }

    public void saveBussiness(Map<String, Object> map) {
        Object platFormIdObj = map.get("platFormId");
        if(platFormIdObj!=null){
            Long platFormId = Long.valueOf(platFormIdObj.toString());
            Map<String, Object> platMap = platformDao.getPlatformInfo(platFormId);
            Object settleCountObject = platMap.get("settleCount");
            if(settleCountObject!=null){
                Integer settleCount = Integer.valueOf(settleCountObject.toString());
                if(settleCount<10){
                    try{
                        platformDao.saveBussiness(map);
                    }catch (Exception e){
                        e.printStackTrace();
                        throw GlobalException.build("商户支付号不能重复");
                    }

                }else {
                    throw GlobalException.build("已经关联了10个，无法继续新增");
                }
            }else {
                throw GlobalException.build("已经关联了10个，无法继续新增");
            }

        }
    }

    public void removeBussiness(Map<String, Object> map) {
        platformDao.removeBussiness(map);
        platformDao.removeBussinessRes(map);
    }
}
